# $Id$

"""
index.py
--------
the index view of sk

"""

__author__ = 'Pat Smith <pat@waldosmith.com'
__docformat__ = 'restructuredtext en'
__version__ = '$Revision$'[11:-2]

from django.template import Context, loader
from django.http import HttpResponse
from monulu.raid.models import  Character
from monulu.sk.models import SKList, SKSuicide


def index(request):
    lists = []
    sklists = SKList.objects.all().order_by('pk')
    for l in sklists:
        listRanks = []
        try:
            raid = l.getLastRaid()
        except IndexError:
            t = loader.get_template('sk/nodata.html')
            return HttpResponse(t.render(Context({})))
        for rank in l.getRanks():
            char = Character.objects.get(pk=rank[0])
            listRanks.extend([RaidRank(rank[0], 
                             char,
                             rank[1], 
                             raid.isInRaid(char),
                             SKSuicide.objects.filter(raid=raid, character=char))])
        lists.append(listRanks)
        
    t = loader.get_template('sk/index.html')
    c = Context({'sklists': sklists,'ranklist':lists})
    return HttpResponse(t.render(c))
